import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'index.dart';
import '../../../common/components/index.dart';

/// 一起玩页面视图
/// 
/// 显示活动列表，支持：
/// - 下拉刷新
/// - 上拉加载更多
class ActivityIndexPage extends GetView<ActivityIndexController> {
  const ActivityIndexPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFF8F9FA),
      appBar: AppBar(
        title: const Text(
          '一起玩',
          style: TextStyle(
            color: Color(0xFF333333),
            fontSize: 17,
            fontWeight: FontWeight.bold,
          ),
        ),
        backgroundColor: Colors.white,
        elevation: 0,
        centerTitle: true,
        iconTheme: const IconThemeData(color: Color(0xFF333333)),
      ),
      body: _buildActivityList(),
    );
  }

  /// 构建活动列表
  Widget _buildActivityList() {
    return Obx(() {
      // 加载中
      if (controller.isLoading.value && controller.activities.isEmpty) {
        return const Center(
          child: CircularProgressIndicator(),
        );
      }
      
      // 空状态
      if (controller.activities.isEmpty) {
        return _buildEmptyState('暂无活动', '下拉可刷新');
      }
      
      // 活动列表 - 使用CustomScrollView
      return RefreshIndicator(
        onRefresh: () => controller.loadActivities(isRefresh: true),
        child: NotificationListener<ScrollNotification>(
          onNotification: (ScrollNotification scrollInfo) {
            if (scrollInfo.metrics.pixels >= scrollInfo.metrics.maxScrollExtent - 200) {
              controller.loadMoreActivities();
            }
            return false;
          },
          child: CustomScrollView(
            slivers: [
              // 活动网格
              SliverPadding(
                padding: EdgeInsets.all(12.r),
                sliver: SliverGrid(
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 2,
                    childAspectRatio: 0.72,
                    crossAxisSpacing: 10.w,
                    mainAxisSpacing: 10.h,
                  ),
                  delegate: SliverChildBuilderDelegate(
                    (context, index) {
                      final activity = controller.activities[index];
                      return ActivityCard(
                        activity: activity,
                        onTap: () => controller.viewActivity(activity.activityId),
                      );
                    },
                    childCount: controller.activities.length,
                  ),
                ),
              ),
              
              // 底部加载提示
              SliverToBoxAdapter(
                child: _buildLoadMoreIndicator(),
              ),
            ],
          ),
        ),
      );
    });
  }

  /// 构建空状态
  Widget _buildEmptyState(String title, String subtitle) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text(
            '📭',
            style: TextStyle(fontSize: 64.sp),
          ),
          SizedBox(height: 16.h),
          Text(
            title,
            style: TextStyle(
              fontSize: 16.sp,
              color: Colors.grey,
            ),
          ),
          SizedBox(height: 8.h),
          Text(
            subtitle,
            style: TextStyle(
              fontSize: 14.sp,
              color: Colors.grey[400],
            ),
          ),
        ],
      ),
    );
  }

  /// 构建加载更多指示器
  Widget _buildLoadMoreIndicator() {
    return Obx(() {
      if (controller.isLoadingMore.value) {
        return Container(
          height: 50.h,
          color: const Color(0xFFF8F9FA),
          child: Center(
            child: SizedBox(
              width: 20.w,
              height: 20.h,
              child: const CircularProgressIndicator(
                strokeWidth: 2,
                valueColor: AlwaysStoppedAnimation<Color>(Color(0xFFD84545)),
              ),
            ),
          ),
        );
      }
      
      if (!controller.hasMoreActivities.value && controller.activities.isNotEmpty) {
        return Container(
          height: 50.h,
          color: const Color(0xFFF8F9FA),
          child: Center(
            child: Text(
              '没有更多了',
              style: TextStyle(
                fontSize: 13.sp,
                color: Colors.grey[400],
              ),
            ),
          ),
        );
      }
      
      return SizedBox(height: 0.h);
    });
  }
}
